﻿using System;
using System.Collections.Generic;
using System.Linq;
using Luna.Data.Budget;
using Luna.Model.Budget;
using Rhino.Mocks;

namespace Luna.Services.Test.Budget
{
    public class BudgetTest : EntityTest<IBudgetProvider, IEditableBudget, Guid, IBudgetData>
    {
        protected override Action<IBudgetProvider, Guid> DataDelete
        {
            get { return (p, k) => p.BudgetDelete(k); }
        }

        protected override Func<IBudgetProvider, Guid, IBudgetData> DataGet
        {
            get { return (p, k) => p.BudgetGet(k); }
        }

        protected override Func<IBudgetProvider, IEnumerable<IBudgetData>> DataGetAll
        {
            get { return p => p.BudgetGetAll(); }
        }

        protected override IBudgetProvider DataProvider
        {
            get { return DataContext.Budget; }
        }

        protected override Action<IBudgetProvider, IBudgetData> DataSave
        {
            get { return (p, i) => p.BudgetSave(i); }
        }

        protected override IEnumerable<IEditableBudget> EntitiesAll
        {
            get { return ApplicationModel.Budget.Budgets.Select(x => x.EditableCopy); }
        }

        protected override Action<IEditableBudget> EntityDelete
        {
            get { return ApplicationModel.Budget.Delete; }
        }

        protected override Action<IEditableBudget> EntitySave
        {
            get { return ApplicationModel.Budget.Save; }
        }

        public override void SetUp()
        {
            base.SetUp();
            ApplicationModel.Budget.Reset();
            PropertyShifters.Add(new PropertyShifter<IEditableBudget, string>(x => x.Name, DataManager.GetString));
            PropertyShifters.Add(new PropertyShifter<IEditableBudget, string>(x => x.Comment, DataManager.GetString));
            PropertyShifters.Add(new PropertyShifter<IEditableBudget, DateTime>(x => x.Start, DataManager.GetDate));
            PropertyShifters.Add(new PropertyShifter<IEditableBudget, DateTime?>(x => x.End, DataManager.GetNullableDate));
            PropertyShifters.Add(new PropertyShifter<IEditableBudget, bool>(x => x.LastVersion, DataManager.GetBool));
            PropertyShifters.Add(new PropertyShifter<IEditableBudget, bool>(x => x.Validated, DataManager.GetBool));
        }

        protected override bool BuildMatch(IEditableBudget P_Entity, IBudgetData P_Data)
        {
            return DataKeyMatch(P_Data.PK, P_Entity) && P_Entity.Name == P_Data.Name;
        }

        protected override IBudgetData CreateMockData()
        {
            return BudgetDataManager.CreateBudgetData();
        }

        protected override Guid DataKey(IBudgetData P_Data)
        {
            return P_Data.PK;
        }

        protected override bool DataKeyMatch(Guid P_Parameter, IEditableBudget P_Match)
        {
            return P_Parameter == P_Match.ID;
        }

        protected override IEditableBudget EntityGet(Guid P_Key)
        {
            return ApplicationModel.Budget.Budgets[P_Key].EditableCopy;
        }

        protected override IBudgetData SaveMatch(IEditableBudget P_Entity)
        {
            return Arg<IBudgetData>.Matches(
                 a => DataKeyMatch(a.PK, P_Entity)
                     && a.Name == P_Entity.Name
                     && a.Comment == P_Entity.Comment
                     && a.Start == P_Entity.Start
                     && a.End == P_Entity.End
                     && a.MainBudgetID == P_Entity.MainBudgetID
                     && a.Validated == P_Entity.Validated
                     && a.LastVersion == P_Entity.LastVersion);
        }
    }
}